package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import com.badibadi.infos.Results;
import com.badibadi.infos.User_Club;
import com.badibadi.infos.User_HideModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

public class ClubPermissionActivity extends BaseActivity {

	private User_Club user_Club;
	private String KEY = "ClubPermissionActivity";
	private String id;
	private CheckBox box0;
	private CheckBox box1;

	private final int MSG_SUCCESSFUL = 2;
	private final int NETWORK_ERROR = 3;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ClubPermissionActivity.this);
				try {
					Utils.showMessage(ClubPermissionActivity.this,
							getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case MSG_SUCCESSFUL:
				Utils.ExitPrgress(ClubPermissionActivity.this);
				if ((Boolean) msg.getData().get(KEY)) {
					Utils.showMessage(ClubPermissionActivity.this,
							getResources().getString(R.string.l_xb53));
					if (box0.isChecked() == true) {
						box0.setChecked(false);
						box1.setChecked(true);
					} else if (box0.isChecked() == false) {
						box0.setChecked(true);
						box1.setChecked(false);
					}
					Constants.is_shuaxin=true;
					finish();
				} else {
					Utils.showMessage(ClubPermissionActivity.this,
							getResources().getString(R.string.l_xb54));
				}
				break;
			case NETWORK_ERROR:
				Utils.ExitPrgress(ClubPermissionActivity.this);
				try {
					Utils.showMessage(ClubPermissionActivity.this, getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.club_permission);

		init();

	}

	/**
	 * 初始化界面和数据
	 */
	private void init() {
		// TODO Auto-generated method stub
		Constants.is_shuaxin=false;
		user_Club = (User_Club) getIntent().getBundleExtra(KEY)
				.getSerializable(KEY);
		box0 = (CheckBox) this.findViewById(R.id.box0);
		box1 = (CheckBox) this.findViewById(R.id.box1);
		this.findViewById(R.id.club_permission_back).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						ClubPermissionActivity.this.finish();
					}
				});
		if (user_Club != null) {
			if (user_Club.getName() != null) {
				((TextView) (this.findViewById(R.id.club_name))).setText(""
						+ user_Club.getName());
				id = user_Club.getId();
				String room_limit = user_Club.getRoom_limit();
				if (room_limit.equals("1")) {
					box0.setChecked(true);
					box1.setChecked(false);
				} else if (room_limit.equals("2")) {
					box0.setChecked(false);
					box1.setChecked(true);
				}

			}
		}
		initCheckBoxClistener();

	}

	private void initCheckBoxClistener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.all_people).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (box0.isChecked() != true)
							setRoom_limit(1);
					}
				});
		this.findViewById(R.id.friends).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (box1.isChecked() != true)
							setRoom_limit(2);
					}
				});
	}

	/**
	 * 修改俱乐部空间权限
	 */
	private void setRoom_limit(final int num) {
		// TODO Auto-generated method stub
		Utils.showPrgress(ClubPermissionActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("cid", id);
				map.put("type", "" + num);
//				   
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "room_limit");
				if (t_content != null) {
//					   
					Results results = Utils.checkResult_NNN(
							getApplicationContext(), t_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {

							Message msg = new Message();
							msg.what = MSG_SUCCESSFUL;
							Bundle bun = new Bundle();
							bun.putBoolean(KEY, results.isRet());
							msg.setData(bun);
							handler.sendMessage(msg);

						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

}
